import 'dart:convert';
import 'dart:developer';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get_storage/get_storage.dart';
import 'package:jxchmz/API/LoginApi.dart';
import 'package:jxchmz/Getx%E5%B7%A5%E5%85%B7/GetxTools.dart';

class VipManage {
  LoginApi loginApi = new LoginApi();
  Dio dio = new Dio();
  var ResponseData;

  // 获取本地token
  configDio() async {
    // 获取本地token
    GetStorage storage = GetStorage();
    String Token = await storage.read("Token");
    print("token:$Token");
    print("账号:${await storage.read("Phone")}");
    print("密码:${await storage.read("Password")}");
    // 构造请求头
    dio.options.contentType = "application/json";
    dio.options.headers['Authorization'] = "Bearer $Token";
    dio.options.connectTimeout = 5000;
    dio.options.receiveTimeout = 3000;
  }

  //获取会员列表
  GetVipList(data) async {
    //配置dio
    await configDio();
    try {
      ResponseData = await dio.get(loginApi.ApiUrl + "/system/vip/list", queryParameters: data);
      if (ResponseData.statusCode == 200) {
        // 请求成功
        ResponseData = json.decode(ResponseData.toString());
        log("数据返回:$ResponseData");
        return ResponseData;
      } else {
        log("请求失败，状态码:${ResponseData.statusCode}");
        GetxTools().ErrorToast("系统异常");
        return "";
      }
    } catch (e) {
      GetxTools().ErrorToast("系统异常");
      return "";
    }
  }

  //新增会员
  AddVip(data) async {
    //配置dio
    await configDio();
    print("传参:$data");
    try {
      ResponseData = await dio.post(loginApi.ApiUrl + "/system/vip", data: data);
      if (ResponseData.statusCode == 200) {
        // 请求成功
        ResponseData = json.decode(ResponseData.toString());
        log("数据返回:$ResponseData");
        return ResponseData;
      } else {
        log("请求失败，状态码:${ResponseData.statusCode}");
        GetxTools().ErrorToast("系统异常");
        return "";
      }
    } catch (e) {
      GetxTools().ErrorToast("系统异常");
      return "";
    }
  }

  //修改会员
  UpdateVip(data) async {
    //配置dio
    await configDio();
    print("传参:$data");
    try {
      ResponseData = await dio.put(loginApi.ApiUrl + "/system/vip", data: data);
      if (ResponseData.statusCode == 200) {
        // 请求成功
        ResponseData = json.decode(ResponseData.toString());
        log("数据返回:$ResponseData");
        return ResponseData;
      } else {
        log("请求失败，状态码:${ResponseData.statusCode}");
        GetxTools().ErrorToast("系统异常");
        return "";
      }
    } catch (e) {
      GetxTools().ErrorToast("系统异常");
      return "";
    }
  }

  //删除会员
  DeleteVip(data) async {
    //配置dio
    await configDio();
    print("传参:$data");
    print("请求地址:${"/system/vip/$data"}");
    try {
      ResponseData = await dio.delete(loginApi.ApiUrl + "/system/vip/$data",);
      if (ResponseData.statusCode == 200) {
        // 请求成功
        ResponseData = json.decode(ResponseData.toString());
        log("数据返回:$ResponseData");
        return ResponseData;
      } else {
        log("请求失败，状态码:${ResponseData.statusCode}");
        GetxTools().ErrorToast("系统异常");
        return "";
      }
    } catch (e) {
      GetxTools().ErrorToast("系统异常");
      return "";
    }
  }
}
